import Vue from 'vue'
import VueRouter from 'vue-router';
import BgIndex from '@/views/bg/index.vue'
import RoleController from "@/views/bg/RoleController.vue";
import PermissionController from "@/views/bg/PermissionController.vue";
import Login from "@/views/Login.vue";
import Test from "@/views/Test.vue";
import Index from "@/views/Index.vue";
import Body from "@/layou/Body.vue";
import Detail from "@/views/apartment/Detail.vue";
import ApartmentIndex from '@/views/bg/apartment/apartment/index.vue'
import StuffIndex from '@/views/bg/stuff/stuff/index.vue'
import FeetypeIndex from '@/views/bg/feetype/feetype/index.vue'
import ForbidIndex from '@/views/bg/forbid/index.vue'
import SecurityIndex from '@/views/bg/security/security/index.vue'
import CancelIndex from '@/views/bg/cancel/cancel'
import {TokenUtil} from "@/util/TokenUtil";
Vue.use(VueRouter)
import {Message} from "element-ui";
import InsertApartmentForm from "@/views/bg/apartment/apartment/InsertApartmentForm.vue";
import CountryIndex from '@/views/bg/country/country/index.vue'
import CityIndex from '@/views/bg/city/city/index.vue'
import RoomIndex from '@/views/bg/room/room/index.vue'
import SchoolIndex from '@/views/bg/school/school/index.vue'
import OrderIndex from '@/views/bg/order/index.vue'
import {goTo} from "@/util/common";
const routes = [
  {
    path: "/",
    component: Index,
    redirect: "/index/home"
  },
  {
    path: "/index",
    component: Index,
    redirect: "/index/home",
    children: [
      {
        path:"home",
        component:Body
      },
      {
        path:"apartment/detail",
        component:Detail
      },
    ]
  },
  {
    path: '/bg',
    component:BgIndex,
    meta:{
      check:true
    },
    children:[
      {
        path:"permission",
        component:PermissionController,
      },
      {
        path:"role",
        component:RoleController,
      },
      {
        path: "apartment",
        component: ApartmentIndex
      },
      {
        path: "stuff",
        component: StuffIndex
      },
      {
        path: "feetype",
        component: FeetypeIndex
      },
      {
        path: "forbid",
        component: ForbidIndex
      },
      {
        path: "security",
        component: SecurityIndex
      },
      {
        path: "cancel",
        component: CancelIndex
      },
      {
        path:"room",
        component: RoomIndex
      },
      {
        path:"apartment/add",
        component: InsertApartmentForm
      },
      {
        path:"country",
        component: CountryIndex
      },
      {
        path:"city",
        component: CityIndex
      },
      {
        path:"school",
        component: SchoolIndex
      },
      {
        path:"order",
        component: OrderIndex
      }
    ]

  },
  {
    path: "/login",
    component: Login
  },
  {
    path: "/test",
    component: Test
  },
]

//添加以下代码
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

const router = new VueRouter({
  mode:"history",
  routes
})
router.beforeEach(((to,from,next)=>{
  let rootPath = to.matched[0]

  if(rootPath && rootPath.meta.check){

    let token = TokenUtil.getToken();
    if(!token){
      Message.error("请先登录")
      next("/login")
    }
  }
  return next()
}))
router.afterEach((to,from)=>{
  if(to.meta.title){
    document.title=to.meta.title
  }
  // goTo(0,0)
  goTo(0,0)
})
export default router
